package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.bjesc_b2c.app.appraiset.widget.popupwindow.BasicDropDownPop;
import com.rxretrofit_build.CarPhotoID;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarBigPhotoStatusBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/2/24.
 * 骨架界面
 */

public class CarFrameworkImageActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.tv_water_tank)
    TextView waterTankTv;
    @InjectView(R.id.tv_left_inner_plate)
    TextView leftInnerPlateTv;
    @InjectView(R.id.tv_left_front_bumper)
    TextView leftFrontBumperTv;
    @InjectView(R.id.tv_right_front_bumper)
    TextView rightFrontBumperTv;
    @InjectView(R.id.tv_right_inner_plate)
    TextView rightInnerPlateTv;
    @InjectView(R.id.tv_footrest)
    TextView footrestTv;
    @InjectView(R.id.tv_left_front_side_bar)
    TextView leftFrontSideBarTv;
    @InjectView(R.id.tv_right_front_side_bar)
    TextView rightFrontSideBarTv;
    @InjectView(R.id.tv_auspicious_bar)
    TextView auspiciousBarTv;
    @InjectView(R.id.tv_left_a)
    TextView leftATv;
    @InjectView(R.id.tv_right_a)
    TextView rightATv;
    @InjectView(R.id.tv_left_b)
    TextView leftBTv;
    @InjectView(R.id.tv_right_b)
    TextView rightBTv;
    @InjectView(R.id.tv_left_c)
    TextView leftCTv;
    @InjectView(R.id.tv_right_c)
    TextView rightCTv;
    @InjectView(R.id.tv_left_rear_side_bar)
    TextView leftRearSideBarTv;
    @InjectView(R.id.tv_right_rear_side_bar)
    TextView rightRearSideBarTv;
    @InjectView(R.id.tv_car_ceiling)
    TextView carCeilingTv;
    @InjectView(R.id.tv_left_rear_trough)
    TextView leftRearTroughTv;
    @InjectView(R.id.tv_right_rear_trough)
    TextView rightRearTroughTv;
    @InjectView(R.id.tv_left_rear_bumper)
    TextView leftRearBumperTv;
    @InjectView(R.id.tv_right_rear_bumper)
    TextView rightRearBumperTv;
    @InjectView(R.id.tv_spare_bar)
    TextView spareBarTv;
    @InjectView(R.id.tv_rear_apron_bar)
    TextView rearApronBarTv;
    @InjectView(R.id.tv_left_rear_inner_plate)
    TextView leftRearInnerPlateTv;
    @InjectView(R.id.tv_right_rear_inner_plate)
    TextView rightRearInnerPlateTv;
    private BasicDropDownPop basicPop;
    private final int startRequest = 88;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_framework_image);
        ButterKnife.inject(this);
        basicPop = new BasicDropDownPop(this,this);
        getFrameworkStatus();
    }

    @OnClick({R.id.iv_return, R.id.tv_save_info, R.id.tv_water_tank, R.id.tv_left_inner_plate,
            R.id.tv_left_front_bumper, R.id.tv_right_front_bumper, R.id.tv_right_inner_plate, R.id.tv_footrest,
            R.id.tv_left_front_side_bar, R.id.tv_right_front_side_bar, R.id.tv_auspicious_bar,
            R.id.tv_left_a, R.id.tv_right_a, R.id.tv_left_b, R.id.tv_right_b, R.id.tv_left_c,
            R.id.tv_right_c, R.id.tv_left_rear_side_bar, R.id.tv_right_rear_side_bar, R.id.tv_car_ceiling,
            R.id.tv_left_rear_trough, R.id.tv_right_rear_trough, R.id.tv_left_rear_bumper,
            R.id.tv_right_rear_bumper, R.id.tv_spare_bar, R.id.tv_rear_apron_bar, R.id.tv_left_rear_inner_plate,
            R.id.tv_right_rear_inner_plate, R.id.btn_previous_page, R.id.btn_next_page,R.id.rlty_title})
    public void onClick(View view) {
        Intent intent = new Intent(CarFrameworkImageActivity.this, CarrosserieDescribeActivity.class);
        switch (view.getId()) {
            case R.id.iv_return:        //返回
                finish();
                break;
            case R.id.tv_save_info:     //保存//已经取消
                break;
            case R.id.tv_water_tank:    //水箱
                buildIntent(intent, false, null, null, null, null, "水箱", null, CarPhotoID.CarFrameworkID.waterTank);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_inner_plate:  //左前内板
                buildIntent(intent, false, null, null, null, null, "左前内板", null, CarPhotoID.CarFrameworkID.leftInnerPlate);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_front_bumper: //左前加强梁
                buildIntent(intent, false, null, null, null, null, "左前加强梁", null, CarPhotoID.CarFrameworkID.leftFrontBumper);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_front_bumper: //右前加强梁
                buildIntent(intent, false, null, null, null, null, "右前加强梁", null, CarPhotoID.CarFrameworkID.rightFrontBumper);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_inner_plate: //右前内板
                buildIntent(intent, false, null, null, null, null, "右前内板", null, CarPhotoID.CarFrameworkID.rightInnerPlate);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_footrest:          //脚板
                buildIntent(intent, false, null, null, null, null, "脚板", null, CarPhotoID.CarFrameworkID.footrest);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_front_side_bar:   //左前外伸梁
                buildIntent(intent, false, null, null, null, null, "左前外伸梁", null, CarPhotoID.CarFrameworkID.leftFrontSideBar);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_front_side_bar:  //右前外伸梁
                buildIntent(intent, false, null, null, null, null, "右前外伸梁", null, CarPhotoID.CarFrameworkID.rightFrontSideBar);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_auspicious_bar:    //元宝梁
                buildIntent(intent, false, null, null, null, null, "元宝梁", null, CarPhotoID.CarFrameworkID.auspiciousBar);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_a:        //左A去
                buildIntent(intent, false, null, null, null, null, "左A区", null, CarPhotoID.CarFrameworkID.leftA);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_a:       //右A区
                buildIntent(intent, false, null, null, null, null, "右A区", null, CarPhotoID.CarFrameworkID.rightA);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_b:        //左B区
                buildIntent(intent, false, null, null, null, null, "左B区", null, CarPhotoID.CarFrameworkID.leftB);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_b:       //右B区
                buildIntent(intent, false, null, null, null, null, "右B区", null, CarPhotoID.CarFrameworkID.rightB);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_c:        //左CD区
                buildIntent(intent, false, null, null, null, null, "左CD区", null, CarPhotoID.CarFrameworkID.leftCD);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_c:       //右CD区
                buildIntent(intent, false, null, null, null, null, "右CD区", null, CarPhotoID.CarFrameworkID.rightCD);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_rear_side_bar:    //左后外伸梁
                buildIntent(intent, false, null, null, null, null, "左后外伸梁", null, CarPhotoID.CarFrameworkID.leftRearSideBar);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_rear_side_bar:   //右后外伸梁
                buildIntent(intent, false, null, null, null, null, "右后外伸梁", null, CarPhotoID.CarFrameworkID.rightRearSideBar);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_car_ceiling:           //车顶
                buildIntent(intent, false, null, null, null, null, "车顶", null, CarPhotoID.CarFrameworkID.ceilin);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_rear_trough:      //左后排水槽
                buildIntent(intent, false, null, null, null, null, "左后排水槽", null, CarPhotoID.CarFrameworkID.leftRearTrough);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_rear_trough:     //右后排水槽
                buildIntent(intent, false, null, null, null, null, "右后排水槽", null, CarPhotoID.CarFrameworkID.rightRearTrough);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_rear_bumper:      //左后加强梁
                buildIntent(intent, false, null, null, null, null, "左后加强梁", null, CarPhotoID.CarFrameworkID.leftRearBumper);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_rear_bumper:     //右后加强梁
                buildIntent(intent, false, null, null, null, null, "右后加强梁", null, CarPhotoID.CarFrameworkID.rightRearBumper);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_rear_inner_plate:     //左后内板
                buildIntent(intent, false, null, null, null, null, "左后内板", null, CarPhotoID.CarFrameworkID.leftRearInnerPlate);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_rear_inner_plate:    //右后内板
                buildIntent(intent, false, null, null, null, null, "右后内板", null, CarPhotoID.CarFrameworkID.rightRearInnerPlate);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_spare_bar:             //备胎板
                buildIntent(intent, false, null, null, null, null, "备胎板", null, CarPhotoID.CarFrameworkID.spareBar);
                startIntent(intent, startRequest);
                break;
            case R.id.tv_rear_apron_bar:        //后裙板
                buildIntent(intent, false, null, null, null, null, "后裙板", null, CarPhotoID.CarFrameworkID.rearApronBar);
                startIntent(intent, startRequest);
                break;
            case R.id.btn_previous_page://上一页
                startIntent(new Intent(CarFrameworkImageActivity.this, CarSourceActivity.class), true);
                break;
            case R.id.btn_next_page:    //下一页内饰
                startIntent(new Intent(CarFrameworkImageActivity.this, CarSurfaceItemActivity.class), true);
                break;
            case R.id.rlty_car_form:        //车源图片
                startIntent(new Intent(CarFrameworkImageActivity.this, CarSourceActivity.class),true);
                break;
            case R.id.rlty_car_Framework:   //骨架
            basicPop.dismiss();
                break;
            case R.id.rlty_car_appearance:  //外观
                startIntent(new Intent(CarFrameworkImageActivity.this,CarSurfaceItemActivity.class),true);
                break;
            case R.id.rlty_car_interior:    //内饰
                startIntent(new Intent(CarFrameworkImageActivity.this, CarOrnamentsItemActivity.class),true);
                break;
            case R.id.rlty_car_damage:      //配置
                startIntent(new Intent(CarFrameworkImageActivity.this, DeviceActivity.class),true);
                break;
            case R.id.rlty_car_comprehensive:   //综合评估
                startIntent(new Intent(CarFrameworkImageActivity.this, ComprehensiveEvaluationActivity.class),true);
                break;
            case R.id.rlty_car_photo:       //合影
                startIntent(new Intent(CarFrameworkImageActivity.this, PhotographsActivity.class),true);
                break;
            case R.id.rlty_title:
                basicPop.showAsDropDown(findViewById(R.id.rlty_title));
                break;
        }
    }

    /**
     * 跳转页面需要带的参数
     *
     * @param intent
     * @param isScore        是否有选择项
     * @param scoreOneName   选项一的名称
     * @param scoreTwoName   选项二的名称
     * @param scoreThreeName 选项三的名称
     * @param scoreFourName  选项四的名称
     * @param title          标题
     * @param scoreTitle     选项的标题
     * @param sourceID       图片ID
     */
    private void buildIntent(Intent intent, boolean isScore, String scoreOneName,
                             String scoreTwoName, String scoreThreeName, String scoreFourName,
                             String title, String scoreTitle, String sourceID) {
        intent.putExtra("isScore", isScore);
        intent.putExtra("scoreOneName", scoreOneName);
        intent.putExtra("scoreTwoName", scoreTwoName);
        intent.putExtra("scoreThreeName", scoreThreeName);
        intent.putExtra("scoreFourName", scoreFourName);
        intent.putExtra("title", title);
        intent.putExtra("scoreTitle", scoreTitle);
        intent.putExtra("sourceID", sourceID);
        intent.putExtra("classID", CarPhotoID.frameworkID);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == startRequest) {
            if (data != null) {
                judgmentStatus(data.getStringExtra("sourceID"), data.getBooleanExtra("isChange", false));
            }
        }
    }

    /**
     * 设置状态
     *
     * @param v
     */
    private void changeYellowFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorWhite));
        v.setBackgroundResource(R.color.colorFFF5A823);
    }

    /**
     * 取消状态
     *
     * @param v
     */
    private void changeWhiteFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorFF030303));
        v.setBackgroundResource(R.drawable.shape_text_frame_bbbbbb);
    }

    /**
     * 获取数据，将来要弄成MVP
     */
    private void getFrameworkStatus() {
        HashMap<String, Object> build = new MapUtil()
                .put("id",  CustomSharedPreferences.getString(this, KeyBean.CARID.valueOf(),KeyBean.FILE_NAME.valueOf()))
                .put("class_id", CarPhotoID.frameworkID)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(this, build).create(IApiActivityRequest.class).getFrameworkStatus(),
                new BaseSubscriber<TokenBean<List<CarBigPhotoStatusBean>>>(this) {
                    @Override
                    public void onSuccess(TokenBean<List<CarBigPhotoStatusBean>> o) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            for (int i = 0; i < o.getData().size(); i++) {
                                judgmentStatus(o.getData().get(i).getAttr_id(), true);
                            }

                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    /**
     * 设置控件状态
     *
     * @param change
     * @param view
     */
    private void setViewStatus(boolean change, TextView view) {
        if (change) {
            changeYellowFrame(view); //设置状态
        } else {
            changeWhiteFrame(view); //取消状态
        }
    }

    /**
     * 判断哪些控件需要改变状态
     *
     * @param status 需要改变的ID
     * @param change true 是设置状态，false 是取消状态
     */
    private void judgmentStatus(String status, boolean change) {
        if(TextUtils.isEmpty(status)){
            return;
        }
        switch (status) {
            case CarPhotoID.CarFrameworkID.waterTank:    //水箱
                setViewStatus(change, waterTankTv);
                break;
            case CarPhotoID.CarFrameworkID.leftInnerPlate:  //左前内板
                setViewStatus(change, leftInnerPlateTv);
                break;
            case CarPhotoID.CarFrameworkID.leftFrontBumper: //左前加强梁
                setViewStatus(change, leftFrontBumperTv);
                break;
            case CarPhotoID.CarFrameworkID.rightFrontBumper: //右前加强梁
                setViewStatus(change, rightFrontBumperTv);
                break;
            case CarPhotoID.CarFrameworkID.rightInnerPlate: //右前内板
                setViewStatus(change, rightInnerPlateTv);
                break;
            case CarPhotoID.CarFrameworkID.footrest:          //脚板
                setViewStatus(change, footrestTv);
                break;
            case CarPhotoID.CarFrameworkID.leftFrontSideBar:   //左前外伸梁
                setViewStatus(change, leftFrontSideBarTv);
                break;
            case CarPhotoID.CarFrameworkID.rightFrontSideBar:  //右前外伸梁
                setViewStatus(change, rightFrontSideBarTv);
                break;
            case CarPhotoID.CarFrameworkID.auspiciousBar:    //元宝梁
                setViewStatus(change, auspiciousBarTv);
                break;
            case CarPhotoID.CarFrameworkID.leftA:        //左A去
                setViewStatus(change, leftATv);
                break;
            case CarPhotoID.CarFrameworkID.rightA:       //右A区
                setViewStatus(change, rightATv);
                break;
            case CarPhotoID.CarFrameworkID.leftB:        //左B区
                setViewStatus(change, leftBTv);
                break;
            case CarPhotoID.CarFrameworkID.rightB:       //右B区
                setViewStatus(change, rightBTv);
                break;
            case CarPhotoID.CarFrameworkID.leftCD:        //左CD区
                setViewStatus(change, leftCTv);
                break;
            case CarPhotoID.CarFrameworkID.rightCD:       //右CD区
                setViewStatus(change, rightCTv);
                break;
            case CarPhotoID.CarFrameworkID.leftRearSideBar:    //左后外伸梁
                setViewStatus(change, leftRearSideBarTv);
                break;
            case CarPhotoID.CarFrameworkID.rightRearSideBar:   //右后外伸梁
                setViewStatus(change, rightRearSideBarTv);
                break;
            case CarPhotoID.CarFrameworkID.ceilin:           //车顶
                setViewStatus(change, carCeilingTv);
                break;
            case CarPhotoID.CarFrameworkID.leftRearTrough:      //左后排水槽
                setViewStatus(change, leftRearTroughTv);
                break;
            case CarPhotoID.CarFrameworkID.rightRearTrough:     //右后排水槽
                setViewStatus(change, rightRearTroughTv);
                break;
            case CarPhotoID.CarFrameworkID.leftRearBumper:      //左后加强梁
                setViewStatus(change, leftRearBumperTv);
                break;
            case CarPhotoID.CarFrameworkID.rightRearBumper:     //右后加强梁
                setViewStatus(change, rightRearBumperTv);
                break;
            case CarPhotoID.CarFrameworkID.leftRearInnerPlate:     //左后内板
                setViewStatus(change, leftRearInnerPlateTv);
                break;
            case CarPhotoID.CarFrameworkID.rightRearInnerPlate:    //右后内板
                setViewStatus(change, rightRearInnerPlateTv);
                break;
            case CarPhotoID.CarFrameworkID.spareBar:             //备胎板
                setViewStatus(change, spareBarTv);
                break;
            case CarPhotoID.CarFrameworkID.rearApronBar:        //后裙板
                setViewStatus(change, rearApronBarTv);
                break;
        }
    }
}
